<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型 ">
          <el-option
            v-for="dict in dict.type.table_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:businessdb:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:businessdb:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:businessdb:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="businessdbList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.table_type" :value="scope.row.type"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="info" />
      <el-table-column label="发布时间" align="center" prop="fbDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fbDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="文件" align="center" prop="fjFile" />-->
      <el-table-column label="排序号" align="center" prop="orderNum" />
<!--      <el-table-column label="${comment}" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:businessdb:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewDataUpdate(scope.row)"
          >表数据</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="clomnhandleUpdate(scope.row)"
          >表字段</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:businessdb:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型 ">
            <el-option
              v-for="dict in dict.type.table_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图标" prop="image">
          <image-upload v-model="form.image"  :limit="1"/>
        </el-form-item>
        <el-form-item label="简介" prop="info">
          <el-input v-model="form.info" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="发布时间" prop="fbDate">
          <el-date-picker clearable
            v-model="form.fbDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排序号" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="0" controls-position="right" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="lolumnopen" width="1000px" append-to-body>
     <Fieldshow  :table-id="tableId"  v-if="lolumnopen" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelcolumn">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="dataopen" width="1200px" append-to-body>
      <Securityincidents  v-if="type =='1'"  ></Securityincidents>
      <Doctrine           v-if="type =='2'"  ></Doctrine>
      <Platform           v-if="type =='3'"  ></Platform>
      <Activedata           v-if="type =='4'"  ></Activedata>
      <Examplewar           v-if="type =='5'"  ></Examplewar>
      <Policie              v-if="type =='6'"  ></Policie>
      <Power              v-if="type =='7'"  ></Power>
      <Submissions              v-if="type =='8'"  ></Submissions>
      <Equipment              v-if="type =='9'"  ></Equipment>
      <Org              v-if="type =='10'"  ></Org>

      <div slot="footer" class="dialog-footer">
        <el-button @click="canceldata">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listBusinessdb, getBusinessdb, delBusinessdb, addBusinessdb, updateBusinessdb } from "@/api/business/businessdb";
import DbColumn from '@/components/DbColumn';
import Fieldshow from '@/components/Fieldshow'
import Securityincidents from '@/components/Securityincidents'
import Doctrine from '@/components/Doctrine'
import Platform  from '@/components/Platform'
import Activedata from '@/components/Activedata'
import Examplewar  from '@/components/Examplewar'
import Policie from '@/components/Policie'
import Power from '@/components/Power'
import Submissions from '@/components/Submissions'
import Equipment from '@/components/Equipment'
import Org  from  '@/components/Org'
export default {
  name: "Businessdb",
 dicts: ['table_type'],
  components: { DbColumn,Securityincidents,Fieldshow,Doctrine ,Platform,Activedata,
    Examplewar,Policie,Power,Submissions,Equipment,Org},
  data() {
    return {
      // 遮罩层
      loading: true,

      lolumnopen: false,
      dataopen:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据库表格数据
      businessdbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        image: null,
        info: null,
        fbDate: null,
        fjFile: null,
        orderNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "图标不能为空", trigger: "blur" }
        ],
        info: [
          { required: true, message: "简介不能为空", trigger: "blur" }
        ],
        fbDate: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
      },
      tableId:"",
      type:""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据库列表 */
    getList() {
      this.loading = true;
      listBusinessdb(this.queryParams).then(response => {
        this.businessdbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelcolumn(){
      this.lolumnopen = false;
    },
    canceldata(){
      this.dataopen = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        image: null,
        info: null,
        fbDate: null,
        fjFile: null,
        orderNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据库";
    },
    viewDataUpdate(row){
      // 表数据
      this.dataopen =true;
      this.type= row.type;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBusinessdb(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据库";
      });
    },
    clomnhandleUpdate(row){
      this.lolumnopen = true;
      this.tableId = row.type
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBusinessdb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusinessdb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据库编号为"' + ids + '"的数据项？').then(function() {
        return delBusinessdb(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/businessdb/export', {
        ...this.queryParams
      }, `businessdb_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
